package com.example.springbootwl08.product.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springbootwl08.common.componet.PageResult;
import com.example.springbootwl08.product.mapper.OrderMapper;
import com.example.springbootwl08.product.mapper.ProductMapper;
import com.example.springbootwl08.product.pojo.dto.OrderPageDTO;
import com.example.springbootwl08.product.pojo.entity.Order;
import com.example.springbootwl08.product.pojo.entity.OrderGoods;
import com.example.springbootwl08.product.pojo.entity.Product;
import com.example.springbootwl08.product.pojo.vo.OrderVO;
import com.example.springbootwl08.product.service.OrderService;
import com.github.pagehelper.PageHelper;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 宗雨
 * @create 2024-02-27 15:04
 * project_name:ssm
 * @Description
 */
@Service
public class OrderServiceImpl  extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private OrderMapper orderMapper;


   /* @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public PageResult selectPage(OrderPageDTO orderPageDTO) {
        PageHelper.startPage(orderPageDTO.getPagenum(), orderPageDTO.getPagesize());


        System.out.println(11111);
        System.out.println(orderPageDTO);
        Page<Order> orderPage =orderMapper.selectPage(orderPageDTO);

        System.out.println(orderPage);


       return  new PageResult(orderPage.getTotal(),orderPage.getRecords());
    }*/

    @Override
    public Order getOrderById(Integer orderId) {
        Order order = orderMapper.getById(orderId);
        return order;
    }

    @Override
    @Transactional
    public OrderVO updateOrder(Order order) {
        //修改套餐
        orderMapper.updateByid(order);

        //根据id查询
        Order byId = orderMapper.getById(order.getOrderId());

        OrderVO orderVO = new OrderVO();
        BeanUtils.copyProperties(byId,orderVO);

        //查询订单关联的菜品
        List<OrderGoods> orderGoods = orderMapper.getOrderGoodsById(order.getOrderId());

        for (OrderGoods orderGood : orderGoods) {
            orderGood.setOrderId(order.getOrderId());
        }

        //封装数据
        orderVO.setGoods(orderGoods);
        return orderVO;
    }


}
